package com.shouhuobao.take.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.view.View;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.home.update.UpgradeBean;
import com.iseastar.guojiang.MainActivity;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.TakeWhetherChargesBean;

import droid.frame.activity.ActivityMgr;
import droid.frame.app.Callback;
import droid.frame.ui.dialog.AppDialog;

/**
 * Created by joker on 2017/8/4.
 * 账号设置界面
 */
public class MySettingsActivity extends BaseActivity2 implements View.OnClickListener {

    private AppDialog appDialog = null;

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_settings_my);
        super.findViewById();
        getAppTitle().setCommonTitle("账号设置");

        //店内物料下载
        findViewById(R.id.shop_info_down).setOnClickListener(this);
        //代收包裹是否收费
        findViewById(R.id.take_whether_charges).setOnClickListener(this);
        // 暂停营业
        findViewById(R.id.person_pause_business).setOnClickListener(this);
        // 更新
        findViewById(R.id.person_update).setOnClickListener(this);
        //切换至火箭侠
        findViewById(R.id.switch_courier).setOnClickListener(this);
        // 退出登录
        findViewById(R.id.person_exit).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //店内物料下载
            case R.id.shop_info_down:
                if (null == AppCache.getUser().getStationId()) {
                    return;
                }
                startActivity(ShopInfoDownActivity.class);
                break;
            //代收包裹是否收费
            case R.id.take_whether_charges:
                if (AppCache.getUser().getStationId() == null) {
                    showToast("您的驿站还没有成功开通，开通后即可查看驿站信息");
                    return;
                }
                showLoadingDialog(null);
                AppHttp.getInstance().getTakePaid();
                break;
            // 暂停营业
            case R.id.person_pause_business:
                DialogMgr.showPauseDialog(getContext());
                break;
            // 更新
            case R.id.person_update:
                showLoadingDialog(null);
                AppHttp.getInstance().takeAppUpgrade(1);
                break;
            //切换至火箭侠
            case R.id.switch_courier:
                showLoadingDialog(null);
                AppHttp.getInstance().switchCourier();
                break;
            // 退出登录
            case R.id.person_exit:
                DialogMgr.showQuit(getContext(), new Callback() {
                    public void onDialogDismiss() {
                        // 清空信息
                        AppCache.setUser(null);
                        AppCache.setOrder(null);
                        AppCache.setStationOpenInfoBean(null);
                        ActivityMgr.finishAll();
                        Intents.startLauncherActivity(getContext());
                    }
                });
                break;
        }
    }

    @Override
    public boolean handleMessage(final Message msg) {
        switch (msg.what) {
            // 版本更新
            case MsgID.station_update:
                cancelLoadingDialog();
                final ReqResult<UpgradeBean> result = JSON.parser(msg.obj, UpgradeBean.class);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            UpgradeBean item = result.getResult();
                            if (item == null || item.getIsNeedUpdate() == 0) {
                                if (msg.arg1 > 0) {
                                    appDialog = DialogMgr.showNoUpdate(getContext());
                                }
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return false;
            //点击代收包裹是否收费返回
            case MsgID.get_take_paid:
                cancelLoadingDialog();
                ReqResult<TakeWhetherChargesBean> reqResult = JSON.parser(msg.obj, TakeWhetherChargesBean.class);
                if (checkLoginStatus(reqResult)) {
                    TakeWhetherChargesBean bean = reqResult.getResult();
                    Intent intent = new Intent(getContext(), TakeWhetherChargesActivity.class);
                    intent.putExtra("item", bean);
                    startActivity(intent);
                } else {
                    showToast(reqResult.getMessage());
                }
                return false;
            //切换至火箭侠
            case MsgID.switch_courier:
                cancelLoadingDialog();
                final ReqResult<UserBean> userResult = JSON.parser(msg.obj, UserBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (userResult.getStatus() == 1001) {
                            DialogMgr.switchCourier(getContext());
                        } else if (checkLoginStatus(userResult)) {
                            // 清空信息
                            AppCache.setUser(null);
                            AppCache.setOrder(null);
                            AppCache.setStationOpenInfoBean(null);
                            UserBean bean = userResult.getResult();
                            AppCache.setUser(bean);
                            Intent intent = new Intent();
                            intent.setClass(MySettingsActivity.this, MainActivity.class);
                            startActivity(intent);
                            for (Activity activity : ActivityMgr.getAllActivitys()) {
                                if (!(activity instanceof MainActivity)) {
                                    activity.finish();
                                }
                            }
                            Intents.startLauncherActivity(getContext());
                        } else {
                            showToast(userResult.getMessage());
                        }
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (appDialog != null) {
            appDialog.dismiss();
        }
    }
}
